package com.qq.controller;

import com.qq.pojo.Message;
import com.qq.pojo.User;
import com.qq.service.ClientSocket;

import javax.swing.*;
import java.awt.*;
import java.net.Socket;

/**
 * @author： Aeolia
 * @Description： TODO
 * @Date： 2021/10/16 23:25
 */
public class LoginMenu extends JFrame {

    JPanel account_jPanel;
    JPanel password_jPanel;
    JPanel btn_jPanel;
    // 布局
    Container container;
    // 事件对象
    JButton login_btn;

    JTextField account_jTextField;
    JPasswordField password_Field;


    public LoginMenu() throws HeadlessException {
        // 设置窗口参数
        this.setTitle("QQ登录");
        this.setSize(500, 300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置布局
        createContentPane(Box.createVerticalBox());
        // 生成组件
        createComponents();
        // 添加事件
        addEvent();

        this.setVisible(true);
    }

    /**
     * 创建布局
     *
     * @param container
     */
    private void createContentPane(Container container) {
        this.container = container;
        this.setContentPane(this.container);
    }

    /**
     * 生成组件
     */
    private void createComponents() {
        createAccountJPanel();
        createPasswordJPanel();
        createBtnJPanel();
    }

    /**
     * 创建账号输入区
     */
    private void createAccountJPanel() {
        account_jPanel = new JPanel();
        JLabel account_jLabel = new JLabel("账号");
        account_jTextField = new JTextField(18);
        account_jTextField.setText("A123456");

        account_jPanel.add(account_jLabel);
        account_jPanel.add(account_jTextField);
        container.add(account_jPanel);
    }

    /**
     * 创建密码输入区
     */
    private void createPasswordJPanel() {
        password_jPanel = new JPanel();
        JLabel account_jLabel = new JLabel("密码");
        password_Field = new JPasswordField(18);
        password_Field.setText("123456");

        password_jPanel.add(account_jLabel);
        password_jPanel.add(password_Field);
        container.add(password_jPanel);
    }

    /**
     * 创建按钮
     */
    private void createBtnJPanel() {
        btn_jPanel = new JPanel();
        login_btn = new JButton("登录");
        btn_jPanel.add(login_btn);
        container.add(btn_jPanel);
    }

    private void addEvent() {
        this.login_btn.addActionListener(e -> {
            Socket socket = ClientSocket.getSocket();
            User loginUser = getLoginUser();
            Message message = ClientSocket.login(loginUser);
            if (null != message) {
                switch (message.getCode()) {
                    case LOGIN_SUCCESS -> JOptionPane.showMessageDialog(this, message.getCode().getExplanation(), "消息", JOptionPane.INFORMATION_MESSAGE);//展示登录界面
                    case LOGIN_FAIL -> JOptionPane.showMessageDialog(this, message.getCode().getExplanation(), "消息", JOptionPane.WARNING_MESSAGE);
                    default ->{
                            JOptionPane.showMessageDialog(this, "程序异常", "消息", JOptionPane.WARNING_MESSAGE);
                    }
                }
            } else {
                ClientSocket.resetSocket();
                JOptionPane.showMessageDialog(this, "程序异常", "消息", JOptionPane.WARNING_MESSAGE);
            }
        });
    }

    private User getLoginUser() {
        String account = account_jTextField.getText();
        String password = new String(password_Field.getPassword());
        return new User(account, password);
    }
}
